﻿
using IBll;
using IDal;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    public class MedicationBll : IMedicationbLL
    {

         public MedicationBll(IBaseDal<Medication> baseDa,IBaseDal<Department> baseD)
        {
            BaseDa = baseDa;
            BaseD = baseD;
        }

        public IBaseDal<Medication> BaseDa { get; }
        public IBaseDal<Department> BaseD { get; }


        public Dto ShowMedication(string? name, int departmentId, int index, int size)
        {
            var medication = BaseDa.GetAllAnync().Where(x=>x.isDelete==false);
            var department = BaseD.GetAllAnync();
            var linq = from a in medication
                       join b in department
                       on a.DepartmentId equals b.DepartmentId
                       select new
                       {
                           a.Name,
                           a.DepartmentId,
                           a.MedicationName,
                           a.State,
                           a.MedicationPhone,
                           a.MedicationDate,
                           a.MedicationId,
                            b.DepartmentNmae,
                            a.Doctor,
                  




                       };

            if (!string.IsNullOrEmpty(name))
            {
                linq = linq.Where(x => x.Doctor.Contains(name));

            }

            if (departmentId>0)
            {
                linq = linq.Where(x => x.DepartmentId.Equals(departmentId));


            }

            int k = linq.Count();
            var w = linq.OrderBy(x => x.MedicationId).Skip((index - 1) * size).Take(size).ToList();
             return  new Dto {Count=k,Mummber=w};
        }
        public async Task<int> AddAnync(Medication medicati)
        {
              return await BaseDa.AddAnync(medicati);

        }

        public Medication FindMedication(int id)
        {
            return BaseDa.GetAllAnync().FirstOrDefault(x => x.MedicationId == id);

        }

        public async Task<int> IsDeleteState(int id)
        {
            var ids = BaseDa.GetAllAnync().FirstOrDefault(x => x.MedicationId == id);
            if (ids.isDelete==true)
            {
                 ids.isDelete = false;
            }
            else
            {
                  ids.isDelete=true;
            }

            return await BaseDa.UpadateAnync(ids);

        }

        public async Task<int> PudataStete(string idstrings)
        {
            List<int> idss = idstrings.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var idd = BaseDa.GetAllAnync().Where(x => idss.Contains(x.MedicationId));
             foreach (var item in idd)
            {

                  item.State=item.State==true?false:true;
            }
            return await BaseDa.UpadateRangeAnync(idd.ToList());


        }

    

        public async Task<int> UpadateAnync(Medication medicati)
        {
            return await BaseDa.UpadateAnync(medicati);
        }

        public List<Medication> GetMedications()
        {
            return BaseDa.GetAllAnync().ToList();


        }
    }
}
